package org.setaria.control
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	import mx.controls.Image;
	import mx.controls.Text;
	import mx.core.FlexGlobals;
	import mx.managers.ISystemManager;
	
	import org.setaria.event.OptionSetariaEvent;
	import org.setaria.skins.control.AlertSkin;
	import org.setaria.utils.SetariaPopUpManager;
	
	import spark.components.BorderContainer;
	import spark.components.Button;
	import spark.components.Label;
	import spark.skins.spark.BorderContainerSkin;

	public class SetariaAlert extends BorderContainer
	{
		public static const CLOSE:int = 1;
		public static const OK_CANCEL:int = 2;
		public static const OK_CANCEL_CLOSE:int = 3;
		public static const YES_NO:int = 4;
		public static const YES_NO_CLOSE:int = 5;
		
		[SkinPart(required="false")]
		public var titleLable:Label;
		[SkinPart(required="false")]
		public var alertIcon:Image;
		[SkinPart(required="false")]
		public var text:Text;
		[SkinPart(required="false")]
		public var closeBtn:Button;
		[SkinPart(required="false")]
		public var okBtn:Button;
		[SkinPart(required="false")]
		public var cancelBtn:Button;
		
		private var btnShow:int = CLOSE;
		
		
		private var title:String;
		private var content:String;
		public var removethis:Function;
		
		public function SetariaAlert(title:String,text:String,buttonFlag:int)
		{
			super();
			this.setStyle("skinClass",AlertSkin)
			this.title = title;
			this.content = text;
			this.btnShow = buttonFlag;
		}

		/**
		 *  @private
		 */
		override protected function partAdded(partName:String, instance:Object) : void
		{
			super.partAdded(partName, instance);
			if(instance == titleLable){
				titleLable.text = title;
			}
			else if(instance == text){
				text.text = content;
			}
//			else if(instance == alertIcon){
//				alertIcon
//			}
			else if(instance == closeBtn)
			{
				closeBtn.addEventListener(MouseEvent.CLICK,closeBtn_clickHandler);
				
				if(btnShow==CLOSE){ 
					closeBtn.label = "确定";
					showCloseBtn();
				}
				else if(btnShow==OK_CANCEL_CLOSE || btnShow == YES_NO_CLOSE)
					showCloseBtn();
					closeBtn.label = "关闭";
			}
			else if(instance == okBtn)
			{
				if(btnShow==OK_CANCEL || btnShow == OK_CANCEL_CLOSE){
					showOkBtn();
					okBtn.addEventListener(MouseEvent.CLICK,okBtn_clickHandler);
					okBtn.label = "确定";
				}
				else if(btnShow==YES_NO || btnShow == YES_NO){
					showOkBtn();
					okBtn.addEventListener(MouseEvent.CLICK,okBtn_clickHandler);
					okBtn.label = "是";
				}
			}
			else if(instance == cancelBtn)
			{
				if(btnShow==OK_CANCEL || btnShow == OK_CANCEL_CLOSE){
					showCancelBtn();
					cancelBtn.addEventListener(MouseEvent.CLICK,cancelBtn_clickHandler);
					cancelBtn.label = "取消";
				}
				else if(btnShow==YES_NO || btnShow == YES_NO_CLOSE){
					showCancelBtn();
					cancelBtn.addEventListener(MouseEvent.CLICK,cancelBtn_clickHandler);
					cancelBtn.label = "否";
				}
			}
			
		}
		
		override protected function partRemoved(partName:String, instance:Object):void
		{
			super.partRemoved(partName, instance);
			if (instance == closeBtn)
				closeBtn.removeEventListener(MouseEvent.CLICK, closeBtn_clickHandler);
			else if (instance == okBtn)
				okBtn.removeEventListener(MouseEvent.CLICK, okBtn_clickHandler);
			else if (instance == cancelBtn)
				cancelBtn.removeEventListener(MouseEvent.CLICK, cancelBtn_clickHandler);
			
		}
		protected function closeBtn_clickHandler(e:MouseEvent):void{
			dispatchEvent(new OptionSetariaEvent(OptionSetariaEvent.CLOSE));
			removethis(this);
		}
		protected function okBtn_clickHandler(e:MouseEvent):void{
			dispatchEvent(new OptionSetariaEvent(OptionSetariaEvent.OK));
			removethis(this);
		}
		protected function cancelBtn_clickHandler(e:MouseEvent):void{
			dispatchEvent(new OptionSetariaEvent(OptionSetariaEvent.CANCEL));
			removethis(this);
		}
		private function showCloseBtn():void{
			this.closeBtn.visible = true;
			this.closeBtn.enabled = true;
			this.closeBtn.includeInLayout = true;
		}
		private function showOkBtn():void{
			this.okBtn.visible = true;
			this.okBtn.enabled = true;
			this.okBtn.includeInLayout = true;
		}
		private function showCancelBtn():void{
			this.cancelBtn.visible = true;
			this.cancelBtn.enabled = true;
			this.cancelBtn.includeInLayout = true;
		}
		
		
		static public function remove(win:SetariaAlert):void{
			SetariaPopUpManager.remove(win);
		}
		static public function show(title:String=null,text:String=null,iconFlag:int=1,buttonFlag:int=1,okHandler:Function=null,cancelHandler:Function=null,closeHandler:Function=null):void{
			var alert:SetariaAlert = new SetariaAlert(title,text,buttonFlag);
			alert.removethis = remove;
			if(okHandler!=null){
				alert.addEventListener(OptionSetariaEvent.OK,okHandler);
			}
			if(cancelHandler!=null){
				alert.addEventListener(OptionSetariaEvent.CANCEL,cancelHandler);
			}
			if(closeHandler!=null){
				alert.addEventListener(OptionSetariaEvent.CLOSE,closeHandler);
			}
			var parent:Sprite;
			var sm:ISystemManager = ISystemManager(FlexGlobals.topLevelApplication.systemManager);
			// no types so no dependencies
			var mp:Object = sm.getImplementation("mx.managers.IMarshallPlanSystemManager");
			if (mp && mp.useSWFBridge())
				parent = Sprite(sm.getSandboxRoot());
			else
				parent = Sprite(FlexGlobals.topLevelApplication);
			SetariaPopUpManager.centerPopup(alert,parent,true);
		}
	}
}